/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package olap4cloud.data;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class GenData {

	private long timeStart;
	private int timeSession = 10;

	/**
	 * Folder File output format
	 */

	private final String fileFormat = "News-%s";

	/**
	 * Date input format
	 */

	private final String dateformat = "yyyy-MM-dd:HH-mm-ss";

	/**
	 * Default number of row that gen data
	 */

	private int numberOfRow = 200000;
	private final int maxRowInFile = 100000;

	public GenData(String timeString, int timeSession, int numberOfRow) {

		try {
			this.timeStart = getTimeLong(timeString);
			System.out.println(timeStart);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.timeSession = timeSession;
		this.numberOfRow = numberOfRow;
	}

	public GenData(String timeString) {

		try {
			this.timeStart = getTimeLong(timeString);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public long getTimeLong(String timeString) throws ParseException {
		SimpleDateFormat simple = new SimpleDateFormat(dateformat);
		return simple.parse(timeString).getTime();
	}

	public String getTime(long timelong) {
		SimpleDateFormat simple = new SimpleDateFormat(dateformat);
		return simple.format(new Date(timelong));
	}

	public String genFileName(long timeStart) {
		return String.format(fileFormat, timeStart);
	}

	public void genDataOneFileDemo(File parrentDir, int counter, int numOfRow)
			throws IOException {
		BufferedWriter br = new BufferedWriter(new FileWriter(new File(
				parrentDir, counter + ".txt")));
		GenDataOverStruct genDataOverStruct = new GenDataOverStruct(timeStart,
				timeStart + timeSession * 1000 * 60);
		for (int i = 0; i < numOfRow; i++) {
			String s = genDataOverStruct.genTimeRequest() + "\t"
					+ genDataOverStruct.genUserId() + "\t"
					+ genDataOverStruct.genDomain() + "\t"
					+ genDataOverStruct.genUrl() + "\t"
					+ genDataOverStruct.genChannelId() + "\t"
					+ genDataOverStruct.genIpRequest() + "\t"
					+ genDataOverStruct.genBrowser() + "\t" 
					+ genDataOverStruct.genCountryId();
			br.write(s + "\n");
		}
		br.close();
	}
	
	public void genDataDemo() {
		// TODO Auto-generated method stub
		File folder = new File (genFileName(timeStart));
		if (folder.exists()){
			System.out.println("Folder exist");
			return;
		}
		try {
			folder.mkdir();
			int counter = 1;
			int rowCreated = 0;
			while (rowCreated < numberOfRow){
				if (rowCreated + maxRowInFile < numberOfRow)
					genDataOneFileDemo(folder, counter, maxRowInFile);
				else
					genDataOneFileDemo(folder, counter, numberOfRow - rowCreated);
				counter ++;
				rowCreated  = rowCreated + maxRowInFile;
				System.out.print("#");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	Random random = new Random();

	public long genTimeRequest(long startTime, long stopTime) {
		return random.nextInt((int) (stopTime - startTime)) + startTime;
	}

	public static void main(String[] args) throws ParseException, IOException {
		if (args.length > 0) {
			GenData genData = new GenData(args[0], Integer.parseInt(args[1]),
					Integer.parseInt(args[2]));
			genData.genDataDemo();
			System.out.println("Finish Gen data demo args...... ");
		} else {
			GenData genData = new GenData("2013-04-16:23-00-00", 10, 200000);
			genData.genDataDemo();
			System.out.println("Finish Gen data demo ...... ");
		}
	}
}
